{extend name="index/index" /}
{block name="main"}
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>


<div class="content-page">

    <div class="content">

        <div class="page-heading">
            <h1><i class='fa fa-table'></i>广告管理</h1>
            <h3>增加广告</h3></div>

        <div class="row">

            <div class="col-md-12">
                <div class="widget"  >
                    <div class="widget-header transparent" style="margin-bottom:30px;">
                        <h2><strong>Sortable</strong> Table</h2>

                    </div>
                    <div class="widget-content">
                        <div >
                            <form name="form1" id="form1"  class="form-horizontal" action="{:url('admin/poster/save')}" method="post" >
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">广告名</label>
                                    <div class="col-sm-5">
                                        <input type="input" class="form-control aaa" id="name" name="poster_name" onblur="a()">

                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">图片</label>
                                    <div class="col-sm-5">
                                       <input type='file'  name="photo" id="photo" />
                                    </div>
                                </div>
                                <div class="form-group" style="margin-bottom:60px;">
                                    <div class="col-sm-offset-2 col-sm-10">
                                        <input type='hidden' name='picname' value='' id='result' placeholder="">
                                        <button class="btn btn-warning" type="button"  class="btn btn-default" onclick="tu()">添加</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>





    </div>


</div>
<script type="text/javascript">


    toastr.options = {  
        closeButton: false,  
        debug: false,  
        progressBar: true,  
        positionClass: "toast-bottom-center",  
        onclick: null,  
        showDuration: "300",  
        hideDuration: "1000",  
        timeOut: "2000",  
        extendedTimeOut: "1000",  
        showEasing: "swing",  
        hideEasing: "linear",  
        showMethod: "fadeIn",  
        hideMethod: "fadeOut"  
    };

//    $('#name').click(function (){
////        var zhi = $('.sqname input').val();
//        alert(2345678);
//    });
    function a(){
        var name = $('#name').val();
        var info = 'name=' + name;
//        alert(info);
        $.ajax({
            type: 'post',
            url: '/admin/poster/nn',
            data: info,
            dataType : 'json',
            success : function (data){

                if (data.status) {
//                    toastr.error('1');

                } else {
                    toastr.error('广告名重复');
                    $('#name').val('');
                }
                // alert(data.status);
            },
            error : function () {
                alert('执行失败');
            }
        });
    }

    function tu(){
        var artName = $('#name').val();
        if(artName.length==0){  

             toastr.error('广告名为空');

        }else{

        var data = new FormData($('#form1')[0]);
        $.ajax({
            url: '/admin/poster/server',
            type: 'POST',
            data: data,
            dataType: 'JSON',
            cache: false,
            processData: false,
            contentType: false
        }).done(function(ret){
            if(ret['isSuccess']){
                // $('#result').html(ret['photo'] );
                document.getElementById('result').value = ret['photo'];
                toastr.success('上传广告成功');
                setTimeout(function fun(){
                    $('#form1').submit();
                },2000)
                // alert('上传成功');
                // return true;
                // $('#form1').submit();

            }else{

                toastr.error('上传广告失败,缺失图片');
                // alert('提交失敗');
                // return false;
            }
        });
       }
    }

  </script>


{/block}